package com.scau.service.impl;

import com.scau.common.exception.BusinessException;
import com.scau.dao.StudentDao;
import com.scau.entity.Student;
import com.scau.service.StudentService;
import com.scau.util.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;
    @Transactional
    @Override
    public void add(Student student) {
        student.setRemainingClass(0);
        Student cache = studentDao.save(student);
        BeanUtils.copyProperties(cache, student);
    }

    @Override
    public Page<Student> findAll(Student student, Pageable pageable, Boolean studying) {

        if (studying == null) {
            return studentDao.findAll(Example.of(student), pageable);
        } else if (studying.equals(Boolean.TRUE)) {

            if (student.getAge() != null) {
                return studentDao.findByRemainingClassGreaterThanAndAge(0,student.getAge(),pageable);
            } else if (student.getName() != null) {
                return studentDao.findByRemainingClassGreaterThanAndName(0, student.getName(), pageable);
            } else if (student.getFollower() != null) {
                return studentDao.findByRemainingClassGreaterThanAndFollower(0,student.getFollower(),pageable);
            } else {
                return studentDao.findByRemainingClassGreaterThan(0, pageable);
            }
        } else {

            if (student.getAge() != null) {
                return studentDao.findByRemainingClassLessThanEqualAndAge(0,student.getAge(),pageable);
            } else if (student.getName() != null) {
                return studentDao.findByRemainingClassLessThanEqualAndName(0, student.getName(), pageable);
            } else if (student.getFollower() != null) {
                return studentDao.findByRemainingClassLessThanEqualAndFollower(0,student.getFollower(),pageable);
            } else {
                return studentDao.findByRemainingClassLessThanEqual(0, pageable);
            }
        }
    }

    @Override
    public Student findOne(Long studentId) {
        Student student = studentDao.findOne(studentId);

        if (student == null) {
            throw new BusinessException("不存在该学员");
        }
        return student;
    }

    @Override
    public Student update(Student student) {
        Student existing = this.findOne(student.getId());
        BeanUtil.copyNonNullProperties(student, existing);
        studentDao.save(existing);
        return existing;
    }

    @Override
    public void delete(Long studentId) {
        studentDao.delete(studentId);
    }

}
